package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.dao.PhotographingMaterialDao;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.PhotographingMaterial;
import com.pgytesting.secondhandcar.service.OrderNameService;
import com.pgytesting.secondhandcar.service.PhotographingMaterialService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合影材料(PhotographingMaterial)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Slf4j
@Service("photographingMaterialService")
public class PhotographingMaterialServiceImpl implements PhotographingMaterialService {
    @Resource
    private PhotographingMaterialDao photographingMaterialDao;

    @Resource
    OrderNameDao orderNameDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PhotographingMaterial queryById(String id) {
        return this.photographingMaterialDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<PhotographingMaterial> queryAllByLimit(int offset, int limit) {
        return this.photographingMaterialDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param photographingMaterial 实例对象
     * @return 对象列表
     */
    @Override
    public List<PhotographingMaterial> queryAll(PhotographingMaterial photographingMaterial) {
        return this.photographingMaterialDao.queryAll(photographingMaterial);
    }

    /**
     * 新增数据
     *
     * @param photographingMaterial 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ResponseMessage insert(PhotographingMaterial photographingMaterial) {
        String phoneNumber = SessionUtil.getSessionMcpCar().getPhoneNumber();
        // 获取到订单ID
        String orderId = photographingMaterial.getId();
        // 生成合影材料id
        photographingMaterial.setId(UtilMethod.getUniqueCode());
        photographingMaterial.setCreatedBy(phoneNumber);
        // 把合影材料id更新到订单对象中
        OrderName orderName = new OrderName();
        orderName.setPhotographingMaterialId(photographingMaterial.getId());
        // 把订单ID更新到订单对象中
        orderName.setId(orderId);
        orderName.setUpdatedBy(phoneNumber);
        // 更新订单状态
        orderName.setOrderStatus(Parameters.ORDER_23001);
        photographingMaterialDao.insert(photographingMaterial);
        orderNameDao.update(orderName);
        return ResponseMessage.successResponse("添加合影信息成功");
    }

    /**
     * 修改数据
     *
     * @param photographingMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public PhotographingMaterial update(PhotographingMaterial photographingMaterial) {
        this.photographingMaterialDao.update(photographingMaterial);
        return this.queryById(photographingMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.photographingMaterialDao.deleteById(id) > 0;
    }
}